from sqlalchemy.orm import Session
from models.permission.permission_model import Permission




def get_permission_by_id(db:Session,id:int) -> Permission:
    permission = db.query(Permission.id,
                          Permission.name,
                          Permission.url,
                          Permission.method,
                          Permission.args,
                          Permission.parent_id,
                          Permission.desc,
                          Permission.create_time).filter(Permission.id == id).first()

    return permission


def get_permission_pagenation(db:Session,page_size:int,current_page:int) -> [Permission]:
    permissions = db.query(
        Permission.id,
        Permission.name,
        Permission.url,
        Permission.method,
        Permission.args,
        Permission.parent_id,
        Permission.desc,
        Permission.create_time
    ).limit(page_size).offset((current_page-1)*page_size).all()
    return permissions

def get_permission_total(db:Session) -> int:
    total = db.query(Permission).count()
    return total

def permission_update(db:Session,id:int,name:str,url:str,method:str,args:str,parent_name:str,desc:str,icon:str):
    permission = db.query(Permission).filter(Permission.id == id).first()
    permission.name = name
    permission.url=url
    permission.method = method
    permission.args = args
    permission.parent_name = parent_name
    permission.desc = desc
    permission.icon=icon
    if parent_name!="无":#表示编辑的结果还是一个一级菜单
        p = db.query(Permission).filter(Permission.name == parent_name).first()
        permission.parent_id = p.id

    db.commit()
    db.flush()


def get_no_parent_names(db:Session,id:int):
    has_permission:Permission=db.query(Permission).filter(Permission.id==id).first()
    if has_permission.parent_id==0: #是一级菜单,剔除自身
        no_parents=db.query(Permission).filter(Permission.id!=has_permission.id).all()
    else:
        no_parents=db.query(Permission).filter(Permission.id!=has_permission.parent_id).all()
    return no_parents

def get_parents_names(db:Session):
    parents=db.query(Permission).all()
    return parents




def delete_permission_by_id(db:Session,id:int):
    permission = db.query(Permission).filter(Permission.id == id).first()
    db.delete(permission)
    db.commit()
    db.flush()


def add_permission(db:Session,name:str,url:str,args:str,method:str,parent_name:str,desc:str,icon:str):
    if parent_name=="无":
        parent_id=0
    else:
        parent:Permission=db.query(Permission).filter(Permission.name==parent_name).first()
        parent_id=parent.id
    permission = Permission(
        name = name,
        url = url,
        desc = desc,
        args = args,
        parent_id=parent_id,
        method=method,
        icon=icon
    )

    db.add(permission)
    db.commit()
    db.flush()


def query_permission(db:Session,q:str,page_size:int,current_page:int) -> [Permission]:

    departments = db.query(
        Permission.id,
        Permission.name,
        Permission.url,
        Permission.method,
        Permission.args,
        Permission.parent_id,
        Permission.desc,
        Permission.create_time
    ).filter(Permission.name.like('%'+q+'%')).limit(page_size).offset((current_page-1)*page_size).all()
    return departments


def get_permission_query_total(db:Session,q:str) -> int:
    total = db.query(Permission).filter(Permission.name.like('%'+q+'%')).count()
    return total
